package com.community.listener;

import java.util.Date;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.community.core.entity.BaseEntity;
import com.es.core.utils.DateUtils;



/**
 * 监听器，监听实体
 * Created by zhanglj
 * Date 2015/11/24.
 */
public class BaseEntityListener {
    private static Logger logger=LoggerFactory.getLogger(BaseEntityListener.class);
    /**
     *
     * @param object
     */
    @PrePersist
    public void prePersist(Object object){
        if(object instanceof BaseEntity){
            BaseEntity baseEntity = (BaseEntity) object;
            // 创建新对�?
            baseEntity.setCreateTime(DateUtils.getCurrentDateTime());
            baseEntity.setLastModifyTime(DateUtils.getCurrentDateTime());
            // baseEntity.setCreateBy(loginName);
        }
    }
    /**
     * Pre update.
     *
     * @param object
     *            the object
     */
    @PreUpdate
    public void preUpdate(Object object) {

        if (object instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) object;
            // 修改旧对�?
            baseEntity.setLastModifyTime(DateUtils.getCurrentDateTime());
            // baseEntity.setLastModifyBy(loginName);
            logger.info("{}对象(ID:{}) �?{} �?{} 修改",
                    new Object[] { object.getClass().getName(), baseEntity.getId(),null, new Date() });
        }
    }

}
